Ext.define('Zm.view.tour.Position',{
    alias:'widget.tourposition',
    extend:'Ext.grid.Panel',
    requires:[
        'Ext.grid.RowNumberer',
        'Zm.model.Position'
    ],
    initComponent:function(){
        var me = this,
            lang = Zm.i18n.Lang;
            
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing',{errorSummary:false});
        me.devid = 0;
        me.store = Ext.create('Ext.data.Store',{model:'Zm.model.Position'});
        Ext.apply(me,{
            title: lang.tTourPos,
            layout:'fit',
            plugins: [me.cellEditing],
            columns:[
                {xtype:'rownumberer',text:lang.tIndex,width:35},
                {text:lang.tPosition,dataIndex:'position',flex:1,editor:'textfield'},
                {text:lang.tOrientation,dataIndex:'orientation',flex:1,editor:'textfield'}                
            ],
            viewConfig : {
                loadMask : false
            },
            dockedItems: [{
                xtype:'toolbar',
                dock:'top',
                items:[{
                    text:lang.tAddPos,
                    iconCls:'posadd',
                    action:'add'
                },{
                    text:lang.tDel,
                    iconCls:'posdel',
                    itemId:'btndel',
                    disabled: true,
                    handler:me.delDetail,
                    scope:me
                }]
            }],
            listeners:{
                edit:me.onEdit,
                selectionchange:me.onSelChange
            }
        });
        
        me.callParent(arguments);
    },
    onSelChange: function(selModel, selections){
        var me = this,
            flag = selections.length === 0,
            btn2 = me.down('#btndel');
        btn2.setDisabled(flag);
    },
    setValue:function(tid){
        var me = this;
        Ext.Ajax.request({
            url: g_app + '/Tour/getPos',
            scope: me,
            params:{tid:tid},
            success:function(response,opts){
                var obj = Ext.decode(response.responseText,true);
                if(obj!=null){
                    me.dept = obj.message;
                    me.store.removeAll();
                    if(obj.data) me.store.add(obj.data);
                };
            }
        });
    },
    addDetail: function(){
        var rd = Ext.create('Zm.model.Position');
        this.store.add(rd);
    },
    delDetail: function(){
        var me = this,
            lang = Zm.i18n.Lang,
            sm = me.getSelectionModel();
        var records = sm.getSelection();
        if(records.length>0){
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg:lang.tDelMsg,
                   buttons: Ext.Msg.YESNO,
                   fn: function(btn){
                       if(btn=='yes') this.store.remove(records);
                   },
                   scope: me,
                   icon: Ext.Msg.QUESTION
               });                        
        }
    },    
    onEdit:function(editor,e,eOpt){
    },
    getValue: function(){
        var me = this,
            details = me.store.getRange(),
            ret = new Array(),
            i,
            n=0;
        me.cellEditing.completeEdit();
        for(i=0;i<details.length;i++){
            ret[n++] = details[i].data;
        }
        return ret;
    }    
});